/*	================================================================================
	$.Example.survey
	Author: 	Philip Poole
				
	Date:		2009-03-26
	Description:Popups a survey to the user
	================================================================================ */
	
(function($) {

	/**
	 * <p>Popups a survey to a user as a AJAX popup.</p>
	 * 
	 * <p>This function creates an in-page Lightbox using a jQuery plugin called Thickbox
	 * and loads content from a third-party survey service using AJAX and an iFrame.</p>
	 * 
	 * <p>The script show the survey about once every 4 pages refreshes rather than every refresh.
	 * This value can be changed from the options.</p>
	 * 
	 * <p>If the survey is shown a cookie is added to the users browser.  This then stops the 
	 * survey from been shown to the user again.  The cookie name is set on a survey by survey basis.</p>
	 * 
	 * <p>For options see $.Example.survey.defaults {@link $.Example.survey.defaults }</p>
	 * 
	 * @param {string} surveyLink The URL of the survey page.
	 * @param {string} cookieName The name of the cookie to set which will prevent the survey from being displayed twice.
	 * @param {array} options See $.Example.survey.defaults for more information.
	 * 
 	 * @example $.Example.survey('http://www.surverymonkey.com/survey/21321', 'survey_200910326', options);
 	 * @class
	 */
	$.Example.survey = function(surveyLink, cookieName, options) {
		//load up the thick box and cookie component
		$.jFrame.useComponents('cookies,thickbox', function () {
			$.Example.survey.init(surveyLink, cookieName, options);
		});
	};
	
	/**
	 * <p>Loads the survey</p>
	 * 
	 * <p>Is executed after thick box is loaded up.</p>
	 * @private
	 * @param {string} surveyLink The URL of the survey page.
	 * @param {string} cookieName The name of the cookie to set which will prevent the survey from being displayed twice.
	 * @param {array} options See $.Example.survey.defaults for more information.
	 */ 
	$.Example.survey.init = function(surveyLink, cookieName, options) {
		$.jFrame.debug("in init" , surveyLink);

		opts = $.extend({}, $.Example.survey.defaults, options);
		//if cookie is not set

		if ($.cookie(cookieName) == null) {	
			//show dialog
			var popupProbability = opts.popupProbability;

			if (Math.random() >= (popupProbability*5)) return;
			
			//set cookie 
			$.cookie(cookieName, 'now_set', { expires: 30 });
		
			$.Example.survey.createModal(opts.title, surveyLink, opts.width, opts.height);
		}
	}


	/**
	* <p>Creates the modal dialog which contains the loaded content. This function uses the Thickbox script.</p>
	* 
	* @param {string} title Text that should appear in the title area of the dialog container.
	* @param {string} url The URL of the survey page.
	* @param {string} w The width of the dialog container.
	* @param {string} h The height of the dialog container.
	* 
	* @private
	*/
	$.Example.survey.createModal = function(title, url, w, h) {
		$.jFrame.debug("create modal");
		url = url + "?TB_iframe=true&amp;height=" + h + "&amp;width=" + w;
		tb_show(title, url, null);		
	}

	/**
	 * <p>The default setting for the survey.  This can be overwritten by inputting options with overwrite these.</p>
	 * <ul>
	 * 	<li>width: The width of the AJAX popup</li>
	 * 	<li>height: The height of the AJAX popup</li>
	 * 	<li>popupProbability: The propbability the popup box will appear to the user, 1 means
	 *  the box would be shown on every page load</li>
	 * 	<li>title: The title of the survey popup.</li>	
	 * </ul>
	 */
	$.Example.survey.defaults = {
		width: 				'400', 				//width of popup box
		height: 			'400', 				//height of popup box
		popupProbability: 	1, 				//probability that popup box will appear to user
		title: 				'Take our survey'  //title of survey
	};
	
})(jQuery);